package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {


        @Reference
        private FactoryService factoryService;

        @RequestMapping(value = "/list",name = "展示厂家列表数据")
        public String findAll(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "5") int pageSize){
            FactoryExample example = new FactoryExample();
            PageInfo<Factory> pageInfo = factoryService.findByPage( example,pageNum,pageSize);
            request.setAttribute("pageInfo",pageInfo);
            return "/cargo/factory/factory-list";
        }

        @RequestMapping(value = "/toAdd",name = "跳转到新增厂家的页面")
        public String toAdd( ){
            return "/cargo/factory/factory-add";
        }

        @RequestMapping(value = "/edit",name = "保存厂家方法")
        public String save(Factory factory){
//        新增 id上是没有值的
//        修改 id是有值的
            if(StringUtils.isEmpty(factory.getId())){ //ID为空 新增
                factory.setId(UUID.randomUUID().toString());
                factory.setCreateTime(new Date());
                factory.setCreateBy(getLoginUser().getId());
                factory.setCreateDept(getLoginUser().getDeptId());
                factoryService.save(factory);
            }else{
                factory.setUpdateBy(getLoginUser().getId());
                factory.setUpdateTime(new Date());
                factoryService.update(factory);
            }
            return "redirect:/cargo/factory/list.do"; //重定向到企业列表中
        }

        @RequestMapping(value = "/toUpdate",name = "进入到厂家编辑页面")
        public String toUpdate(String id){
            Factory factory = factoryService.findById(id);
            request.setAttribute("factory",factory);
            return "/cargo/factory/factory-add";
        }
        @RequestMapping(value = "/delete",name = "删除厂家数据")
        public String delete(String id){
            factoryService.delete(id);
            return "redirect:/cargo/factory/list.do";
        }
}
